<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $db = \DB::table("user");
       $list = $db->paginate(5); //5条每页浏览
        
       return view("admin.user.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.user.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       //获取要添加的数据
        $data = $request->only("userName",'password','email','question','answer');
		$data['password'] = md5($data['password']);
		
        //执行添加
        $id = \DB::table("user")->insertGetId($data);
        //判断
        if($id>0){
            $info = "用户信息添加成功！";
        }else{
            $info = "用户信息添加失败！";
        }
        
        return view("admin.user.info",['info'=>$info]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
		$data = \DB::table('user')->where('id',$id)->first();
		//设置无头像时的默认头像
		if($data->photo == null){
			$data->photo = "default.jpg";
		}
        return view("admin/user/show",['list'=>$data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = \DB::table("user")->where("id",$id)->first();
		//设置无头像时的默认头像
		if($data->photo == ""){
			$data->photo = "default.jpg";
		}
		return view("admin.user.edit",['list'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
		if($request->password !== $request->surepassword){
			$info = "密码输入不一致！";
			return view("admin.user.info",['info'=>$info]);
		}
		//将获取的密码与原密码进行比对
		$olddata = \DB::table('user')->where("id",$id)->first();
		if($olddata->password !== $request->password){
			//对密码进行加密
			$request->password = md5($request->password);
		}
		
		//获取用户头像
		//使用自定义文件上传类处理上传
		$upfile = new \App\Org\Upload("photo");
		//初始化上传信息
		$upfile->path="./user/uploads"; //上传存储路径
		$upfile->typeList=array("image/jpeg","image/png","image/gif"); //设置允许上传类型
		$upfile->maxSize=0; //允许上传大小
	
		//执行文件上传
		$upfile->doUpload();
		$photo = $upfile->saveName;
		
		//进行数据库更新操作
        \DB::table("user")->where("id",$id)->update(['userName'=>$request->userName,"password"=>$request->password,"email"=>$request->email,"status"=>$request->status,"photo"=>$photo,"question"=>$request->question,"answer"=>$request->answer]);
		//获取所有的数据库用户信息
		$data = \DB::table("user");
		$list = $data->paginate(5); //5条每页浏览
		return view("admin.user.index",['list'=>$list]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
		/* $data = \DB::table('user')->where('id',$id)->first();
		if($data->photo != "default.jpg"){
			unlink("user/uploads/{$data->photo}");
		} */
        \DB::table('user')->delete($id);
        return redirect("admin/user");
    }
	
    public function dong($id)
    {
		$id = intval($id);
		\DB::table("user")->where("id",$id)->update(["status"=>1]);
      
        return redirect("admin/user");
    }
	
    public function jdong($id)
    {
		$id = intval($id);
		\DB::table("user")->where("id",$id)->update(["status"=>0]);
      
        return redirect("admin/user");
    }
}
